<template>
<div class="container">
  <div class="help">
  <transition name="enter">
    <div class="repoter" v-if="isDetail">
      <div class="country">
        <div class="countryBox">{{income.country}}</div>
        <span>
          <div class="countNum">0</div>
          <div>组合数</div>
        </span>
      </div>
      <div class="oMask">
        <div class="income-header">
          <div class="title">
            近一年风险系数：{{income.risk | vToFixedTwo}}
          </div>
          <div class="seeDetail" @click="gotoDetail">查看详情</div>
        </div>
        <div class="income-box">
          <div class="income">
            <i class="iconfont" :class="[ income.mr < 0 ? 'icon-xia' : 'icon-shang' ]"></i>
            <a>{{income.mr | vToFixedTwo}}<sub>%</sub></a>
            <span>近一月收益率</span>
          </div>
          <div class="bar"></div>
          <div class="income">
            <i class="iconfont" :class="[ income.yr < 0 ? 'icon-xia' : 'icon-shang' ]"></i>
            <a>{{income.yr | vToFixedTwo}}<sub>%</sub></a>
            <span>近一年收益率</span>
          </div>
        </div>
      </div>
    </div>
  </transition>
  </div>
  <div class="boxContainer">
  <div class="box" :class="{ moveMap : isMove }">
    <div class="qsmap"
      style="transition: all 0.7s ease-in-out;"
      :class="{ active : active, isMove: isMove }"
      :style="{ transform: 'translate3D(-' + mapCoordinate.x + 'px,-' + mapCoordinate.y + 'px,0)' }">
      <div class="mapMask"
        :style="{ top: mapCoordinate.y + 80 + 'px', left: mapCoordinate.x + 80 + 'px' }"></div>
      <span v-for="(item, idx) in point"
        :key="idx"
        :class="{ pointActive : item.active}"
        :style="{ left: item.point.x * scaling.x + 'px', top: item.point.y * scaling.y + 'px' }" 
        class="point">
        <i>
          <span :class="{ movePoint : !isDetail }"></span>
        </i>
      </span>
    </div>
  </div>
  </div>
</div>
</template>

<script>
let oWidth = 0

export default {
  name: 'qstragery-map',
  props: {
    coordinate: {
      type: Object,
      default: {
        x: 0,
        y: 0
      }
    },
    active: {
      type: Boolean,
      default: false
    },
    point: {
      type: Array,
      default: [{
        x: 0,
        y: 0,
        active: false
      }]
    },
    country: {
      type: Object,
      default: {}
    }
  },
  data () {
    return {
      globalRem: 1,
      isDetail: false,
      scaling: {
        x: 1,
        y: 1
      },
      count: 1,
      pointerMove: false,
      isMove: false,
      income:  {
        "size_name": "",
        "_id": "",
        "currency": "",
        "type": "",
        "yr": 0,
        "risk": 0,
        "updated_time": "",
        "mr": 0,
        "index_name": "",
        "country": "",
        "feature": ""
      },
      hidden: false,
      mapCoordinate: {
        x: 0,
        y: 0
      },
      deviation: {
        x: 0,
        y: 0
      },
      startDeviation: {
        x: 0,
        y: 0
      },
      currentCoordinate: {
        x: 0,
        y: 0
      }
    }
  },
  watch: {
    '$store.state.selectedDQstrage': function (val) {
      this.income = val
    },
    '$store.state.qstrageryDetail': function (val) {
      this.isDetail = val
    },
    '$store.state.qstrageryByCountry': function (val) {
      this.point.forEach((item) => {
        item.active = false
      })
      this.point[val].active = true
      val === 3 ? this.hidden = true : this.hidden = false
    },
    coordinate: function (val) {
      let xScal = 960 / oWidth // 放大后的高度 除 原始高度
      let yScal = 720 / 180 // 放大后的宽度 除 原始宽度
      this.scaling = {
        x: xScal,
        y: yScal
      }
      this.mapCoordinate = {
        x: Math.floor(this.coordinate.x * this.scaling.x) - 100,
        y: Math.floor(this.coordinate.y * this.scaling.y) - 135
      }
      this.isMove = true
      setTimeout(() => {
        this.isMove = false
      }, 450)
    },
    active: function (val) { // 地图X Y轴的缩放比例
      let xScal = 960 / oWidth // 放大后的高度 除 原始高度
      let yScal = 720 / 180 // 放大后的宽度 除 原始宽度
      if (val) {
        this.scaling = {
          x: xScal,
          y: yScal
        },
        this.mapCoordinate = {
          x: Math.floor(this.coordinate.x * this.scaling.x) - 100,
          y: Math.floor(this.coordinate.y * this.scaling.y) - 135
        }
      } else {
        this.scaling = this.mapCoordinate = {
          x: 1,
          y: 1
        }
        this.point.forEach((item) => {
          item.active = false
        })
        this.$store.state.qstrageryByCountry = 0
        this.$store.state.qstrageryDetail = false
      }
    }
  },
  methods: {
    seeDetail (idx) {
      if (idx + 1 === this.$store.state.qstrageryByCountry) return
      this.$emit('seeDetail', idx)
      this.$set(this.$store.state, 'qstrageryDetail', true)
      this.isMove = true
      this.$store.state.qstrageryByCountry = idx + 1
    },
    gotoDetail () {
      this.$emit('gotoDetail')
    },
    dragMapStart (e) {
      if (!this.active) return
      e.target.style.transition = ''
      this.startDeviation = {
        x: e.touches[0].clientX,
        y: e.touches[0].clientY
      }
      let str = e.target.style.transform
      this.currentCoordinate = {
        x: parseInt(str.replace(/translate3d\((\-?\d+)px,\s*(\-?\d+)px,\s*(\d+)px\)/, "$1")),
        y: parseInt(str.replace(/translate3d\((\-?\d+)px,\s*(\-?\d+)px,\s*(\d+)px\)/, "$2"))
      }
    },
    dragMapMove (e) {
      if (!this.active) return
      e.preventDefault();
      this.point.forEach((item) => {
        item.active = false
      })
      this.$store.state.qstrageryByCountry = 0
      this.deviation = {
        x: e.touches[0].clientX - this.startDeviation.x + this.currentCoordinate.x,
        y: e.touches[0].clientY - this.startDeviation.y + this.currentCoordinate.y
      }
      this.$set(this.mapCoordinate, 'x', this.deviation.x)
      this.$set(this.mapCoordinate, 'y', this.deviation.y)
      e.target.style.transform = `translate3d(${Math.floor(this.mapCoordinate.x)}px, ${Math.floor(this.mapCoordinate.y)}px, 0)`
      this.$set(this.$store.state, 'qstrageryDetail', false)
    },
    dragMapEnd (e) {
      if (!this.active) return
      e.target.style.transition = 'all 0.7s ease-in-out'
    }
  },
  created () {
    this.point.forEach((item) => {
      this.$set(item, 'active', false)
    })
    oWidth = document.body.offsetWidth
    if (this.$route.params.id) {
      this.isDetail = true
      this.$store.state.qstrageryDetail = true
    }
  }
}
</script>

<style lang="less" scoped>
.iconfont {
  position: absolute;
  top: 8px;
  left: 30px;
  font-size: 2.5rem;
}

.icon-shang {
  color: #F84949;
  margin-left: -12px;
}

.icon-xia {
  color: #7DE28E;
  margin-left: -12px;
}

@media screen and (max-width:375px) and (min-width:321px) {
.icon-shang {
  margin-left: 0
}

.icon-xia {
  margin-left: 0
}
}
@media screen and (min-width: 376px){
.icon-shang {
  margin-left: 12px
}

.icon-xia {
  margin-left: 12px
}
}

.mapMask {
  position: absolute;
  top: 0;
  transition: all 0.7s;
  left: 0;
  height: 500px;
  margin-top: -200px;
  margin-left: -500px;
  width: 2000px;
  background-image: linear-gradient(-178deg, rgba(255,255,255,1) 40%, rgba(124,148,246,0.4) 100%);
}
.help {
  overflow: hidden;
  width: 100%;
  height: 200px;
  transition: all 0.7s ease-in-out;
  position: absolute;
  z-index: 5;
  .repoter {
    z-index: 99;
    position: absolute;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    top: 0;
    left: 0;
    .country {
      position: absolute;
      top: 105px;
      box-shadow: 0 2px 3px 0 rgba(0,0,0,0.11);
      left: 69px;
      height: 2.3rem;
      width: 11rem;
      line-height: 2rem;
      flex: 1;
      flex-direction: row;
      background: #FA6262;
      @media screen and (max-width:375px) and (min-width:321px) {
        height: 2.4rem;
        width: 12rem;
        left: 64px;
        line-height: 2.2rem;
      }
      @media screen and (min-width: 376px){
        height: 2.5rem;
        line-height: 2.4rem;
        width: 13rem;
        left: 65px; 
      }
      .countryBox {
        flex: 1;
        background: white;
        height: 100%;
        display: inline-block;
        justify-content: center;
        align-items: center;
        letter-spacing: 4px;
        width: 60%;
        text-align: center;
        flaot: right;
        font-size: 1.4rem;
      }
      span {
        flex: 1;
        height: 100%;
        display: inline-flex;
        flex-direction: column;
        justify-content: center;
        font-size: 1.2rem;
        transform: translate3d(0, -4px, 0);
        width: 35%;
        text-align: center;
        flaot: right;
        align-items: center;
        color: white;
        div {
          flex: 1;
          height: 50%;
          width: 100%;
          background: transparent;
          display: inline;
        }
        div:nth-child(1) {
          font-size: 1.2em;
        }
        div:nth-child(2) {
          font-size: 0.7em;
        }
      }
    }
    .oMask {
      height: 0px;
      width: 100%;
      .income-header {
        display: flex;
        background: white;
        justify-content: space-between;
        padding: 10px 1.2rem 0 1.2rem;
        .seeDetail {
          background-image: linear-gradient(-232deg, #91A5F8 0%, #6379F8 100%);
          border-radius: 10px 0 0 10px;
          padding: 0.3rem 1.1rem 0.3rem 1.8rem;
          margin-right: -1.2rem;
          display: flex;
          font-size: 1.2rem;
          align-items: center;
          height: 2.2rem;
          color: white;
          letter-spacing: 2px;
          @media screen and (max-width:375px) and (min-width:321px) {
            height: 2.3rem;
            border-radius: 15px 0 0 15px;
            font-size: 1.3rem;
          }
          @media screen and (min-width: 376px){
            height: 2.4rem;
            border-radius: 20px 0 0 20px;
            font-size: 1.4rem;
          }
        }
        .title {
          font-size: 1.2rem;
          display: flex;
          padding-left: 15px;
          justify-content: center;
          align-items: center;
          @media screen and (max-width:375px) and (min-width:321px) {
            font-size: 1.4rem;
          }
          @media screen and (min-width: 376px){
            font-size: 1.6rem;
          }
        }
      }
      .income-box {
        width: 80%;
        display: flex;
        flex-direction: row;
        .income {
          display: flex;
          position: relative;
          flex-direction: column;
          flex: 1;
          align-items: flex-end;
          a {
            font-size: 2.4rem;
          }
          span {
            font-size: 1.3rem;
            margin-right: 0px;
          }
        }
      }
    }
  }
}

.movePoint {
  height: 12px !important;
  width: 12px !important;
  border: 2px solid white;
}

.boxContainer {
  overflow: hidden;
  width: 100%;
  height: 200px;
  transition: all 0.3s;
  position: relative;
  @media screen and (min-width: 376px){
    height: 235px;
  }
}

.bar {
  background: #eee;
  height: 35px;
  width: 3px;
  border-radius: 5px;
  margin: auto 0;
  transform: translate3D(15px, 0, 0);
}

.box {
  overflow: hidden;
  width: 100%;
  height: 100%;
  transition: all 0.7s ease-in-out;
  position: relative;
  background: #E6EAFE;
  .qsmap {
    height: 180px;
    width: 100%;
    display: block;
    position: relative;
    background-image: url('../../assets/img/qsmap.jpg') !important;
    background-size: 100% 100%; 
  }
  .point {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 999;
    height: 3.4rem;
    width: 3.4rem;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    i {
      top: 0;
      left: 0;
      height: 1.8rem;
      width: 1.8rem;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        background: #546EFF;
        height: 0.8rem;
        width: 0.8rem;
        border-radius: 50%;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
}

.pointActives {
  height: 2rem !important;
  width: 2rem !important;
}

.pointActive {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 999;
  background: rgba(100,127,255,0.28);
  border-radius: 50%;
  border: 1px solid #6580FF;
  animation: point 1s 1 0.5s;
  display: flex;
  justify-content: center;
  align-items: center;
  i {
    top: 0;
    left: 0;
    height: 1rem;
    width: 1rem;
    background: white;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    transition: all 0.5s;
    align-items: center;
    span {
      height: 0.6rem;
      width: 0.6rem;
      border-radius: 50%;
      display: flex;
    }
  }
}

.moveMapPoint {
  transition: all 0.35s;
}

.active {
  height: 720px !important;
  width: 960px !important;
}

.isMove {
  height: 648px !important;
  width: 864px !important;
}
.moveMap {
  padding-left: 72px;
  padding-top: 96px;
  margin-left: -36px;
  margin-top: -48px;
}

@keyframes point
{
0% {
  height: 2.4em;
  width: 2.4em;
  margin-top: 0;
  margin-left: 0;
}
50% {
  height: 1.5em; 
  width: 1.5em;
  margin-top: 0;
  margin-left: 0;
}
100% {
  height: 2.4em; 
  width: 2.4em;
  margin-top: 0;
  margin-left: 0;
}
}

</style>